package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.util.LockUtil;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * Author：汤小洋
 * Date：2025-02-08 9:10
 * Description：<描述>
 */
@RestController
@RequiredArgsConstructor
public class LockController {

    private final StringRedisTemplate redisTemplate;
    private final RedissonClient redissonClient;

    /**
     * 下单时减库存
     */
    // @RequestMapping("/deductStock")
    // public String deductStock(){
    //     System.out.println(Thread.currentThread().getName() + "线程正在下单。。。。。。。。。。。。。。。。。。");
    //
    //     synchronized (this){
    //         int total = Integer.parseInt(redisTemplate.opsForValue().get("stock"));
    //         if (total > 0){
    //             try {
    //                 Thread.sleep(5000); // 休眠
    //             } catch (InterruptedException e) {
    //                 throw new RuntimeException(e);
    //             }
    //             total = total - 1;
    //             redisTemplate.opsForValue().set("stock", total + "");
    //             return "下单成功，库存还有：" + total;
    //         }
    //     }
    //     return "下单失败，库存不足";
    // }

    // @RequestMapping("/deductStock")
    // public String deductStock(){
    //     System.out.println(Thread.currentThread().getName() + "线程正在下单。。。。。。。。。。。。。。。。。。");
    //
    //     String lockName = "stock_lock"; // 锁的名称
    //     String value = UUID.randomUUID().toString();
    //     if(! LockUtil.getLock(lockName,value)){
    //         return "获取锁失败";
    //     }
    //
    //     int total = Integer.parseInt(redisTemplate.opsForValue().get("stock"));
    //     if (total > 0){
    //         try {
    //             Thread.sleep(5000); // 休眠
    //         } catch (InterruptedException e) {
    //             throw new RuntimeException(e);
    //         }
    //         total = total - 1;
    //         redisTemplate.opsForValue().set("stock", total + "");
    //         // 释放锁
    //         LockUtil.unlock(lockName,value);
    //         return "下单成功，库存还有：" + total;
    //     }
    //     LockUtil.unlock(lockName,value);
    //     return "下单失败，库存不足";
    // }

    @RequestMapping("/deductStock")
    public String deductStock(){
        System.out.println(Thread.currentThread().getName() + "线程正在下单。。。。。。。。。。。。。。。。。。");

        // 使用Redisson实现分布式锁
        String lockName = "stock_lock"; // 锁的名称
        RLock rLock = redissonClient.getLock(lockName);
        rLock.lock();

        int total = Integer.parseInt(redisTemplate.opsForValue().get("stock"));
        if (total > 0){
            try {
                Thread.sleep(5000); // 休眠
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            total = total - 1;
            redisTemplate.opsForValue().set("stock", total + "");
            // 释放锁
            rLock.unlock();
            return "下单成功，库存还有：" + total;
        }
        rLock.unlock();
        return "下单失败，库存不足";
    }
}
